<template>
  <el-dialog width="60%" :title="title" style="" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="司机姓名" prop="name">
            <el-input v-model="dataForm.name" placeholder="司机姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="dataForm.status"
              placeholder="司机状态"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="dict in dict.type.sys_normal_disable"
                :key="Number(dict.value)"
                :label="dict.label"
                :value="Number(dict.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-select style="width: 100%;" v-model="dataForm.sex" placeholder="性别" clearable>
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="Number(dict.value)"
                :label="dict.label"
                :value="Number(dict.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="locationId">
            <el-input v-model="dataForm.locationId" placeholder="地址"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="驾龄(年)" prop="drivingExperience">
            <el-input v-model="dataForm.drivingExperience" placeholder="驾龄"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号" prop="idNumber">
            <el-input v-model="dataForm.idNumber" placeholder="身份证号"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="工号" prop="jobNumber">
            <el-input disabled v-model="dataForm.jobNumber" placeholder="工号"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="身份证反面照" prop="idImage">
            <image-upload ref="idImage" :type="'image'"
              :imageLimitSize="4"
              :imageSize="1"
              :imgageList="idImageList" @updateImg="changeImg('1',$event)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证正面照" prop="idImage2">
            <image-upload ref="idImage2" :type="'image'"
              :imageLimitSize="4"
              :imageSize="1"
              :imgageList="idImage2List" @updateImg="changeImg('2',$event)" />
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
          <el-form-item label="驾驶证正面照" prop="licenseImage">
            <image-upload ref="licenseImage"
              :type="'image'"
              :imageLimitSize="4"
              :imageSize="1"
              :imgageList="licenseImageList" @updateImg="changeImg('3',$event)" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    addOrUpdate
  } from "@/api/business/driverinfo";
  import {statusList} from '@/vars/var.config.js'
  export default {
    components: {},
    dicts: ['sys_user_sex','sys_normal_disable'],
    data() {
      return {
        imageApi:process.env.VUE_APP_IMAGE_BASE_URL,
        statusList,
        franchiseeList: [],
        idImageList: [], //身份证
        idImage2List: [], //身份证国徽
        licenseImageList: [], //驾驶证正面
        licenseImage2List: [], //驾驶证反面
        title: '',
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          mobile: '',
          wxUnionid: '',
          nickName: '',
          avatarUrl: '',
          remark: '',
          createTime: '',
          createBy: '',
          updateBy: '',
          updateTime: '',
          delFlag: '',
          delTime: '',
          status: '',
          miniappOpenid: '',
          birthday: '',
          sex: '',
          locationId: '',
          balance: '',
          idNumber: '',
          idImage: '',
          idImage2: '',
          idPeriod: '',
          licenseImage: '',
          licenseImage2: '',
          drivingExperience: '',
          jobNumber: '',
          password: ''
        },
        dataRule: {
          name: [{
            required: true,
            message: '用户名不能为空',
            trigger: 'blur'
          }],
          mobile: [{
            required: true,
            message: '手机号不能为空',
            trigger: 'blur'
          }],
          wxUnionid: [{
            required: true,
            message: '微信id不能为空',
            trigger: 'blur'
          }],
          nickName: [{
            required: true,
            message: '昵称不能为空',
            trigger: 'blur'
          }],
          franchiseeId: [{
            required: true,
            message: '加盟商ID不能为空',
            trigger: 'blur'
          }],
          remark: [{
            required: true,
            message: '备注不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '状态 1:启用，2：禁用不能为空',
            trigger: 'blur'
          }],
          sex: [{
            required: true,
            message: '性别：1男，2女不能为空',
            trigger: 'blur'
          }],
          locationId: [{
            required: true,
            message: '地址不能为空',
            trigger: 'blur'
          }],
          balance: [{
            required: true,
            message: '余额不能为空',
            trigger: 'blur'
          }],
          idNumber: [{
            required: true,
            message: '身份证号不能为空',
            trigger: 'blur'
          }],
          idImage: [{
            required: true,
            message: '身份证反面照不能为空',
            trigger: 'blur'
          }],
          idImage2: [{
            required: true,
            message: '身份证正面照不能为空',
            trigger: 'blur'
          }],
          idPeriod: [{
            required: true,
            message: '证件有效期不能为空',
            trigger: 'blur'
          }],
          licenseImage: [{
            required: true,
            message: '驾驶证正面照不能为空',
            trigger: 'blur'
          }],
          licenseImage2: [{
            required: true,
            message: '驾驶证反面照不能为空',
            trigger: 'blur'
          }],
          drivingExperience: [{
            required: true,
            message: '驾龄（年）不能为空',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '登陆密码不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    mounted(){
      // this.init()
    },
    methods: {
      changeImg(val, e) {
        if (val == '1') {
          this.idImageList = e
        } else if (val == '2') {
          this.idImage2List = e
        } else if (val == '3') {
          this.licenseImageList = e
        } else if (val == '4') {
          this.licenseImage2List = e
        }
      },
      init(record) {
        this.visible = true
        this.dataForm = record
        if(this.dataForm){
          if(this.dataForm.idImage){
            this.$nextTick(() => {
              this.idImageList=[{
                    uid: this.dataForm.id,
                    id: this.dataForm.id,
                    status: "success",
                    filePath: this.dataForm.idImage,
                    name: this.dataForm.idImage,
                    url: this.imageApi + this.dataForm.idImage
                  }];
              this.$refs.idImage.init(this.idImageList)
            })
          }
          if(this.dataForm.idImage2){
            this.$nextTick(() => {
              this.idImage2List=[{
                    uid: this.dataForm.id,
                    id: this.dataForm.id,
                    status: "success",
                    filePath: this.dataForm.idImage2,
                    name: this.dataForm.idImage2,
                    url: this.imageApi + this.dataForm.idImage2
                  }];
              this.$refs.idImage2.init(this.idImage2List)
            })
          }
          if(this.dataForm.licenseImage){
            this.$nextTick(() => {
              this.licenseImageList=[{
                    uid: this.dataForm.id,
                    id: this.dataForm.id,
                    status: "success",
                    filePath: this.dataForm.licenseImage,
                    name: this.dataForm.licenseImage,
                    url: this.imageApi + this.dataForm.licenseImage
                  }];
              this.$refs.licenseImage.init(this.licenseImageList)
            })
          }
          if(this.dataForm.licenseImage2)
          this.licenseImage2List=[{
                uid: this.dataForm.id,
                id: this.dataForm.id,
                status: "success",
                filePath: this.dataForm.licenseImage2,
                name: this.dataForm.licenseImage2,
                url: this.imageApi + this.dataForm.licenseImage2
              }];
        }
        if (this.dataForm && this.dataForm.id) {
          this.title = '司机信息表' + '修改'
        } else {
          this.title = '司机信息表' + "新增"
        }
      },
      // 表单提交
      dataFormSubmit() {
        this.dataForm.idImage = this.idImageList[0]?.filePath
        this.dataForm.idImage2 = this.idImage2List[0]?.filePath
        this.dataForm.licenseImage = this.licenseImageList[0]?.filePath
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addOrUpdate(this.dataForm).then(res => {
              this.visible = false
              this.$modal.msgSuccess(this.title + "成功");
              this.$emit('refreshDataList')
            })
          }
        })
      }
    }
  }
</script>
